package com.hx.resource;

import com.hx.common.core.domain.AjaxResult;
import com.hx.resource.utils.AliyunOSSUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * 通用请求处理
 */
@RestController
@RequestMapping("/common")
public class CommonController {


    @Autowired
    private AliyunOSSUtils fileUploadService;





    /**
     * 通用上传请求（单个）
     */
    @PostMapping("/upload")
    public AjaxResult uploadFile(MultipartFile file) throws Exception {
        try {
//            // 上传文件路径
//            String filePath = SystemConfig.getUploadPath();
//            // 上传并返回新文件名称
//            String fileName = FileUploadUtils.upload(filePath, file);
//            String url = serverConfig.getUrl() + fileName;

            String httpPath = fileUploadService.uploadFileInputSteam(file);
            AjaxResult ajax = AjaxResult.success();
            ajax.put("url", "https://" + httpPath);
//            ajax.put("fileName", fileName);
            String newName = httpPath.substring(httpPath.lastIndexOf("/"));
            ajax.put("newFileName", newName);
            ajax.put("originalFilename", file.getOriginalFilename());
            return ajax;
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }



}
